package com.banciyuan.bcywebview.biz.main.mineinfo;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.InterestUser;

/**
 * Created by hy on 2016/6/12.
 */
public class AddFriendsAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<InterestUser> data;
    private ImageLoader mImageLoader;
    private ListView mListView;
    private int headerViewCount;

    public AddFriendsAdapter(Context context, List<InterestUser> data, ListView listView) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        this.data = data;
        mImageLoader = ImageLoader.getInstance();
        mListView = listView;
        headerViewCount = mListView.getHeaderViewsCount();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        final InterestUser user = data.get(position);
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.user_work_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (StringUtil.notNullStartWithUrlHead(user.getAvatar()))
            mImageLoader.displayImage(user.getAvatar(), viewHolder.ivHeader, ImageOptions.options);
        else
            viewHolder.ivHeader.setImageResource(R.drawable.user_pic_big);
        if (user.isFollow_status())
            viewHolder.tvFocusStatus.setVisibility(View.INVISIBLE);
        else
            viewHolder.tvFocusStatus.setVisibility(View.VISIBLE);
        viewHolder.tvName.setText(user.getUname());
        if (!TextUtils.isEmpty(user.getSelf_intro()))
            viewHolder.tvDesc.setText(Html.fromHtml(user.getSelf_intro()));
        setMaxNameWidth(viewHolder, user);
        processPostData(viewHolder, user);
        OnClick onClick = new OnClick(position);
        viewHolder.viewHeader.setOnClickListener(onClick);
        viewHolder.tvFocusStatus.setOnClickListener(onClick);
        return convertView;
    }

    private void processPostData(ViewHolder viewHolder, InterestUser user) {
        InterestUser.UserPost userPost;
        int size = user.getPosts().size() < 3 ? user.getPosts().size() : 3;
        for (int i = 0; i < size; i++) {
            userPost = user.getPosts().get(i);
            OnClick onClick = new OnClick(userPost);
            if ("cos".equals(userPost.getPost_type()) || "illust".equals(userPost.getPost_type())) {
                viewHolder.picViews[i].setVisibility(View.VISIBLE);
                viewHolder.writeViews[i].setVisibility(View.GONE);
                mImageLoader.displayImage(userPost.getCover(), viewHolder.picViews[i], ImageOptions.options);
                viewHolder.picViews[i].setOnClickListener(onClick);
            } else if ("novel".equals(userPost.getPost_type())) {
                viewHolder.picViews[i].setVisibility(View.GONE);
                viewHolder.writeViews[i].setVisibility(View.VISIBLE);
                mImageLoader.displayImage(userPost.getCover(), viewHolder.writeObjs[i].ivCover, ImageOptions.options);
                viewHolder.writeObjs[i].tvTitle.setText(Html.fromHtml(userPost.getTitle()));
                String workName = userPost.getWork();
                if (TextUtils.isEmpty(workName)) {
                    viewHolder.writeObjs[i].workIcon.setVisibility(View.GONE);
                    viewHolder.writeObjs[i].tvWork.setText(mContext.getString(R.string.original));
                } else {
                    viewHolder.writeObjs[i].workIcon.setVisibility(View.VISIBLE);
                    viewHolder.writeObjs[i].tvWork.setText(workName);
                }
                viewHolder.writeViews[i].setOnClickListener(onClick);
            }
        }
        if (size < 3) {
            for (int i = 2; i >= size; i--) {
                viewHolder.picViews[i].setVisibility(View.INVISIBLE);
                viewHolder.writeViews[i].setVisibility(View.INVISIBLE);
            }
            int visibility = size == 0 ? View.GONE : View.VISIBLE;
            viewHolder.viewContainer.setVisibility(visibility);
        }
    }

    private void setMaxNameWidth(ViewHolder viewHolder, InterestUser user) {
        int tagWidth = 0;
        viewHolder.viewTagContainer.removeAllViews();
        for (int i = 0; i < user.getRoles().size(); i++) {
            tagWidth += UIUtils.dip2px(36, mContext);
            TextView tvAuthorTag = (TextView) mLayoutInflater.inflate(R.layout.author_tag, viewHolder.viewTagContainer, false);
            tvAuthorTag.setText(user.getRoles().get(i).getType_name());
            viewHolder.viewTagContainer.addView(tvAuthorTag);
        }
        viewHolder.tvName.setMaxWidth(CommonUtils.getWidthPixels((Activity) mContext) - tagWidth - UIUtils.dip2px(150, mContext));
    }

    class ViewHolder {
        View viewHeader;
        ImageView ivHeader;
        TextView tvName;
        TextView tvDesc;
        TextView tvFocusStatus;
        ImageView[] picViews;
        View[] writeViews;
        ViewWrite[] writeObjs;
        LinearLayout viewTagContainer;
        View viewContainer;

        public ViewHolder(View convertView) {
            viewHeader = convertView.findViewById(R.id.view_header);
            viewContainer = convertView.findViewById(R.id.view_container);
            ivHeader = (ImageView) convertView.findViewById(R.id.user_head);
            tvName = (TextView) convertView.findViewById(R.id.user_name);
            tvDesc = (TextView) convertView.findViewById(R.id.user_desc);
            tvFocusStatus = (TextView) convertView.findViewById(R.id.user_focus);
            viewTagContainer = (LinearLayout) convertView.findViewById(R.id.viewtag_container);
            picViews = new ImageView[]{(ImageView) convertView.findViewById(R.id.user_work_pic_one),
                    (ImageView) convertView.findViewById(R.id.user_work_pic_two), (ImageView) convertView.findViewById(R.id.user_work_pic_three)};
            View writeViewOne = convertView.findViewById(R.id.write_view_one);
            View writeViewTwo = convertView.findViewById(R.id.write_view_two);
            View writeViewThree = convertView.findViewById(R.id.write_view_three);

            writeViews = new View[]{writeViewOne, writeViewTwo, writeViewThree};
            writeObjs = new ViewWrite[]{new ViewWrite(writeViewOne), new ViewWrite(writeViewTwo), new ViewWrite(writeViewThree)};
        }
    }

    static class ViewWrite {
        ImageView ivCover;
        TextView tvTitle;
        TextView tvWork;
        View workIcon;

        public ViewWrite(View viewParent) {
            ivCover = (ImageView) viewParent.findViewById(R.id.iv_write_cover);
            tvTitle = (TextView) viewParent.findViewById(R.id.tv_write_title);
            tvWork = (TextView) viewParent.findViewById(R.id.tv_write_work);
            workIcon = viewParent.findViewById(R.id.iv_work_icon);
        }
    }

    class OnClick implements View.OnClickListener {

        InterestUser.UserPost userPost;
        int position;

        public OnClick(int position) {
            this.position = position;
        }

        public OnClick(InterestUser.UserPost userPost) {
            this.userPost = userPost;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_work_pic_one:
                case R.id.user_work_pic_two:
                case R.id.user_work_pic_three:
                    if ("cos".equals(userPost.getPost_type())) {
                        DetailDataHelper.gotoCosDetail(mContext, userPost.getRp_id(), userPost.getCp_id(), false);
                    } else if ("illust".equals(userPost.getPost_type())) {
                        DetailDataHelper.gotoDrawerDetail(mContext, userPost.getRp_id(), userPost.getDp_id(), false);
                    }
                    break;
                case R.id.write_view_one:
                case R.id.write_view_two:
                case R.id.write_view_three:
                    DetailDataHelper.gotoWriterDetail(mContext, userPost.getRp_id(), userPost.getWp_id(), false);
                    break;
                case R.id.user_focus:
                    TimelineLikeHelper.doFollow(mContext, "dofollow", data.get(position).getUid(), new TimelineLikeHelper.FollowSuccess() {
                        @Override
                        public void onFollowSuccess(String response) {
                            data.get(position).setFollow_status(true);
                            updateView(position + headerViewCount, data.get(position));
                        }
                    });
                    break;
                case R.id.view_header:
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, data.get(position).getUid());
                    break;
            }
        }
    }

    private void updateView(int itemIndex, InterestUser user) {
        if (mListView == null) return;
        View convertView = mListView.getChildAt(itemIndex - mListView.getFirstVisiblePosition());
        if (convertView == null) return;
        ViewHolder viewHolder = null;
        if (convertView.getTag() instanceof ViewHolder)
            viewHolder = (ViewHolder) convertView.getTag();
        if (null == viewHolder)
            return;
        if (user.isFollow_status())
            viewHolder.tvFocusStatus.setVisibility(View.INVISIBLE);
        else
            viewHolder.tvFocusStatus.setVisibility(View.VISIBLE);
    }
}
